package 双指针;

/**
 *
 * @ClassName: Doublepointer
 * @Description: TODO
 * @Author WN
 * @Date 2021/7/13 15:32
 * @Version V1.0
 */
public class Doublepointer {
    //快慢指针
    ListNode detectCycle(ListNode head){
        ListNode fast,slow;
        fast=slow=head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if (fast==slow){
                break;
            }
        }
        if (fast==null|| fast.next==null){
            return null;
        }
        slow=head;
        while (slow!=fast){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;
    }
    //二分查找法  有序的
    int binarySearch(int[] nums,int target){
        int left=0;
        int right=nums.length-1;
        while (left<=right){
            int mid=(right+left)/2;
            if (nums[mid]==target){
                return mid;
            }else if (nums[mid]<target){
                left=mid+1;

            }else if(nums[mid]>target){
                right=mid-1;
            }
        }
        return -1;
    }
    //反转数组
    int[] reverse(int[] nums){
        int left=0;
        int right=nums.length-1;
        while (left<right){
            int temp=nums[left];
            nums[left]=nums[right];
            nums[right]=temp;
            left++;
            right--;
        }
        return nums;
    }
    public static void main(String[] args) {
        Doublepointer doublepointer=new Doublepointer();
        int i = doublepointer.binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7}, 5);
        System.out.println(i);
        int[] reverse = doublepointer.reverse(new int[]{1, 2, 3});
        for (int i1 : reverse) {
            System.out.println(i1);
        }
    }

}
